# 使用langchain框架去实现Prompt---（Prompts）

from openai import OpenAI
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate

api_key = "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"

client = OpenAI(
    # This is the default and can be omitted
    api_key=api_key
)

def get_completion(prompt, model="gpt-3.5-turbo"):
    response = client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": prompt,
            }
        ],
        model=model,
    )
    return response.choices[0].message.content


def get_openai():
    customer_email = """
    Arrr, I be fuming that me blender lid \
    flew off and splattered me kitchen walls \
    with smoothie! And to make matters worse,\
    the warranty don't cover the cost of \
    cleaning up me kitchen. I need yer help \
    right now, matey!
    """
    style = """American English \
    in a calm and respectful tone
    """
    prompt = f"""Translate the text \
    that is delimited by triple backticks 
    into a style that is {style}.
    text: ```{customer_email}```
    """
    print(get_completion(prompt))


def get_langchain_template_1():
    template_string = """Translate the text \
    that is delimited by triple backticks \
    into a style that is {style}. \
    text: ```{text}```
    """
    customer_style = """American English \
    in a calm and respectful tone
    """
    customer_email = """
    Arrr, I be fuming that me blender lid \
    flew off and splattered me kitchen walls \
    with smoothie! And to make matters worse, \
    the warranty don't cover the cost of \
    cleaning up me kitchen. I need yer help \
    right now, matey!
    """

    prompt_template = ChatPromptTemplate.from_template(template_string)

    customer_messages = prompt_template.format_messages(
        style=customer_style,
        text=customer_email)

    chat = ChatOpenAI(api_key=api_key, temperature=0.0)
    customer_response = chat(customer_messages)
    print(customer_response.content)

def get_langchain_template_2():
    review_template = """\
    For the following text, extract the following information:

    gift: Was the item purchased as a gift for someone else? \
    Answer True if yes, False if not or unknown.

    delivery_days: How many days did it take for the product \
    to arrive? If this information is not found, output -1.

    price_value: Extract any sentences about the value or price,\
    and output them as a comma separated Python list.

    Format the output as JSON with the following keys:
    gift
    delivery_days
    price_value

    text: {text}
    """

    customer_review = """\
    This leaf blower is pretty amazing.  It has four settings:\
    candle blower, gentle breeze, windy city, and tornado. \
    It arrived in two days, just in time for my wife's \
    anniversary present. \
    I think my wife liked it so much she was speechless. \
    So far I've been the only one using it, and I've been \
    using it every other morning to clear the leaves on our lawn. \
    It's slightly more expensive than the other leaf blowers \
    out there, but I think it's worth it for the extra features.
    """

    prompt_template = ChatPromptTemplate.from_template(review_template)

    customer_messages = prompt_template.format_messages(
        text=customer_review)

    chat = ChatOpenAI(api_key=api_key, temperature=0.0)
    customer_response = chat(customer_messages)
    print(customer_response.content)

if __name__ == '__main__':
    # get_openai()
    # get_langchain_template_1()
    get_langchain_template_2()
